﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

using SharedGenomics.Workbench.Core;

namespace SharedGenomics.Workbench.Tests.TestObjects
{
    public class TestBackgroundExecutor : IBackgroundExecutor
    {
        public void Execute(SharedGenomics.Core.ICommand command)
        {
            command.Execute();
        }

        public void Execute(SharedGenomics.Core.ICommand command, Action<BackgroundOperationResult> callback)
        {
            this.Execute(() => command.Execute(), callback);
        }

        public void Execute(Action command)
        {
            command();
        }

        public void Execute(Action command, Action<BackgroundOperationResult> callback)
        {
            BackgroundOperationResult opResult = null;
            try
            {
                command();
                opResult = new BackgroundOperationResult();
            }
            catch (Exception ex)
            {
                opResult = new BackgroundOperationResult(ex);
            }

            Debug.Assert(opResult != null);
            callback(opResult);
        }

        public void Execute<T>(Func<T> command, Action<BackgroundOperationResult<T>> callback)
        {
            BackgroundOperationResult<T> opResult = null;
            try
            {
                T result = command();
                opResult = new BackgroundOperationResult<T>(result);
            }
            catch (Exception ex)
            {
                opResult = new BackgroundOperationResult<T>(ex);
            }

            Debug.Assert(opResult != null);
            callback(opResult);
        }
    }
}
